package sparrowGui
{
	import flash.display.Sprite;
	import flash.text.StyleSheet;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	
	import sparrowGui.item.CListItem;
	import sparrowGui.item.Item;
	import sparrowGui.item.ListItem;
	import sparrowGui.item.RadioItem;
	import sparrowGui.utils.PubFun;
	import sparrowGui.vars.*;

	/**
	 * 编辑器样式文件
	 * 通过此类编辑默认的皮肤样式
	 * 设置为单例的用意是方便扩展时直接继承此类,然后覆盖方法改生成函数.
	 * @author Pelephone
	 */	
	public class SparrowGUI
	{
		////////////////////////////////////////
		// 静态参数
		////////////////////////////////////////
		public static var mainDisp:Sprite;
		public static var stageWidth:int;
		public static var stageHeight:int;
		
		protected static var instance : SparrowGUI; 
		
		public function SparrowGUI(s:Sprite=null,sw:int=0,sh:int=0)
		{
			mainDisp = s;
			stageWidth = sw;
			stageHeight = sh;
			if (instance != null) throw Error("此单例已经存在了");
			instance = this;
		}
		
		//获取单例
		public static function getIns():SparrowGUI {
			if (instance == null) instance = new SparrowGUI( );
			return instance;
		}
		
/*		//初始
		public static function init(s:Sprite,sw:int,sh:int):void
		{
			mainDisp = s;
			stageWidth = sw;
			stageHeight = sh;
		}*/

		//////////////////////////////////////////////////
		// get/set/方法
		/////////////////////////////////////////////////
		
		//文本的css样式
		public static var _txtCss:StyleSheet;
		private static const cssStr:String = "" +
			"label{color:#e3dccc; display:inline;}" +
			"btn{font-size:12;text-align:center;font-family:Verdana}" +
			"listItem{text-align:center;}" +
			"title{font-weight:bold}" +
			"tab{color:#e3dccc;}" +
			"labelItem{text-align:center;color:#000000}" +
			"test{text-decoration:normal; font-family:'Times New Roman';  font-style:normal; font-weight:bold}"
			;
		public static function get txtCss():StyleSheet
		{
			if(!_txtCss){
				_txtCss = new StyleSheet();
				_txtCss.parseCSS(cssStr);
			}
			return _txtCss;
		}
		
		/*************************************************************
		 *  基本组件默认样式
		 * @return ComponentCssVO
		 */		
		public function get componentVar():ComponentVars
		{
			var _component:ComponentVars = new ComponentVars();
			_component.setBorder(1);
			_component.setBorderColor(0x111111);
			_component.bgColor = 0xFFFFFF;
			return _component;
		}
		
		/*************************************************************
		 * 窗口组件的css默认样式
s		 */
		// 窗体背景颜色样式
		public function get window_bg():ComponentVars
		{
			var vars:ComponentVars = new ComponentVars();
			vars.bgColor = 0xFFFFFF;
			vars.setBorder(1);
			vars.setBorderColor(0x9c9c9c);
			vars.width = 100;
			return vars;
		}
		// 窗体标题栏颜色样式
		public function get window_titBar():ComponentVars
		{
			var vars:ComponentVars = new ComponentVars();
			vars.bgColor = 0xF3F3F3;
			vars.setBorder(1);
			vars.height = 20;
			vars.setBorderColor(0x9c9c9c);
			return vars;
		}
		
		/**
		 * 默认富文本(带表情功能的文本)皮肤信息
		 */
		public function get richTextTxt():TextField
		{
			var ttf:TextField = new TextField();
			ttf.multiline = true;
			ttf.wordWrap = true;
			ttf.width = ttf.height = 80;
			return ttf;
		}
		
		/*************************************************************
		 * 按钮弹起\按下\经过默认样式
		 */
		public function get textAreaVar():TextAreaVar
		{
			var vars:TextAreaVar = new TextAreaVar();
			vars.width = 100;
			vars.height = 80;
			return vars;
		}
		

		/*************************************************************
		 * 按钮弹起\按下\经过默认样式
		 */
		public function get btn_up():ComponentVars
		{
			var vars:ComponentVars = new ComponentVars();
			vars.setBorder(1);
			vars.setBorderColor(0xcccccc);
			return vars;
		}
		public function get btn_over():ComponentVars
		{
			var vars:ComponentVars = new ComponentVars();
			vars.bgColor = 0xAAAAAA;
			vars.setBorder(1);
			vars.setBorderColor(0xcccccc);
			return vars;
		}
		public function get btn_select():ComponentVars
		{
			var vars:ComponentVars = new ComponentVars();
			vars.bgColor = 0x777777;
			vars.setBorder(1);
			vars.setBorderColor(0xcccccc);
			return vars;
		}
		public function get btn_down():ComponentVars
		{
			var vars:ComponentVars = new ComponentVars();
			vars.bgColor = 0xAAAAAA;
			vars.setBorder(1);
			vars.setBorderColor(0xcccccc);
			return vars;
		}
		public function get btn_base():ComponentVars
		{
			var vars:ComponentVars = new ComponentVars();
			vars.width = 16;
			vars.height = 16;
			vars.isBgFill = true;
			return vars;
		}
		public function get baseBtnVar():ButtonVars
		{
			var _baseBtn:ButtonVars = new ButtonVars();
			var btnDown:ComponentVars = new ComponentVars();
			var btnOver:ComponentVars = new ComponentVars();
			_baseBtn.setBorder(1);
			_baseBtn.setBorderColor(0xA1A1A1);
			
			btnDown.setBorder(1);
			btnDown.bgColor = 0xF5F5F5;
			btnDown.setBorderColor(0xA1A1A1);
			
			btnOver.setBorder(1);
			btnOver.setBorderColor(0xA1A1A1);
			
			_baseBtn.mouseDown = btnDown;
			_baseBtn.mouseOver = btnOver;
			
			_baseBtn.width = btnDown.width = btnOver.width = 60;
			_baseBtn.height = btnDown.height = btnOver.height = 22;

			return _baseBtn;
		}
		
		/*************************************************************
		 * 提示窗样式
		 * @return 
		 */	
		public function get treeVars():VHListVars
		{
			var vars:VHListVars = new VHListVars();
			vars.setBorder(1);
			vars.itemClass = Item;
//			vars.bgColor = 0x222222;
			vars.setBorderColor(0x666666);
			vars.setPadding(2,2,2,2);
			vars.width = 100;
			vars.height = 100;
			vars.spacing = 1;
			vars.colNum = 1;
			vars.autoSort = true;
			vars.multiSelect = true;
			vars.itemClass = RadioItem;
			vars.indet = 5;
//			vars.autoWidth = true;
			return vars;
		}
		
		/*************************************************************
		 * 提示窗样式
		 * @return 
		 */		
		public function get alertCss():AlertVars
		{
			var tip:AlertVars = new AlertVars();
//			tip.bgColor = 0xFAFAFA;
//			tip.setBorder(1,1,1,1);
//			tip.setBorderColor(0x666666);
//			tip.width = 180;
//			tip.height = 100;
//			tip.setPadding(5,5,5,5);
			return tip;
		}
		
		public function get tipVar():OverTipVars
		{
			var tip:OverTipVars = new OverTipVars();
			tip.alpha = 0.8;
			tip.bgColor = 0x000001;
//			tip.setBorder(1);
			tip.setPadding(5);
			tip.width = tip.height = 0;
			tip.isBgFill = true;
			tip.delay = 500;
			tip.showType = 0;
			tip.spacing = 5;
			return tip;
		}
		
		/*************************************************************
		 *  基本组件默认样式
		 * @return ComponentCssVO
		 */		
		public function get comboBoxVar():ComboBoxVars
		{
			var cbb:ComboBoxVars = new ComboBoxVars();
			cbb.itemClass = CListItem;
			cbb.autoHeight = true;
			cbb.autoWidth = true;
			cbb.mustSelect = true;
			return cbb;
		}
		
		/*************************************************************
		 * 列表默认样式
		 */
		public function get listVar():ListVars
		{
			var list:ListVars = new ListVars();
			list.itemClass = ListItem;
			list.setBorder(1);
			list.bgColor = 0;
			list.alpha = 0;
			list.setBorderColor(0x666666);
//			list.autoWidth = true;
//			list.scrollBarVar = scrollBarVar;
//			list.itemClass = ListItem;
//			list.itemVar = itemVar;
//			list.itemVar.itemTag = "listItem"
//			list.itemVar.overVar = itemVar;
//			list.itemVar.overVar.bgColor = 0xCCCCCC;
//			list.itemVar.selectVar = itemVar;
//			list.itemVar.selectVar.bgColor = 0x999999;
//			list.setPadding(2);
			list.width = 100;
			list.height = 100;
			list.spacing = 2;
			list.autoSort = true;
			return list;
		}
		
		/*************************************************************
		 * 列表默认样式
		 */
		public function get radioVar():ListVars
		{
			var rgVar:ListVars = new ListVars();
			rgVar.spacing 		= 10;
			rgVar.width 		= 120;
			rgVar.height 		= 120;
			rgVar.bgColor		= 0;
			rgVar.itemClass		= RadioItem;
			rgVar.setBorder(0);
			rgVar.setBorderColor(0);
			return rgVar;
		}
		
		/*************************************************************
		 * Checkbox复选框默认样式
		 */
		public function get checkboxVar():ListVars
		{
			var cb:ListVars = radioVar;
			cb.itemClass 	= RadioItem;
			cb.multiSelect	= true;
			return cb;
		}
		
		/*************************************************************
		 * 表格默认样式
		 */
		public function get vhlist():VHListVars
		{
			var list:VHListVars = new VHListVars();
			list.setBorder(1);
			list.itemClass = Item;
			list.bgColor = 0x222222;
			list.setBorderColor(0x666666);
			list.setPadding(2,2,2,2);
			list.width = 100;
			list.height = 100;
			list.spacing = 1;
			list.autoSort = true;
			return list;
		}
		
		/*************************************************************
		 * 滚动条默认样式
		 * @return 
		 */
		public function get scrollBarVar():ComponentVars
		{
			var vars:ComponentVars = new ComponentVars();
			vars.bgColor = 0xCCCCCC;
			vars.setBorder(1);
			vars.setBorderColor(0x666666);
			vars.width = vars.height = 14;
			return vars;
		}
		
		/*************************************************************
		 * 默认label项的参数
		 * @return 
		 */
		public function get labelItemVars():BaseComponentVars
		{
			var vars:BaseComponentVars = new BaseComponentVars();
			vars.width = 50;
			vars.isBgFill = true;
			return vars;
		}
		
		/**
		 * 提示工具默认皮肤
		 */
		public function get toolTipSkin():Sprite 
		{
			var skin:Sprite = new Sprite();
			var txtTip:TextField;
			var skinBG:Sprite = new Sprite();
			skinBG.graphics.beginFill(0xFF0000,0.5);
			skinBG.graphics.drawRect(0,0,10,10);
			skinBG.graphics.endFill();
			skinBG.name = "skinBG";
			txtTip = new TextField();
			txtTip.name = "txtTip";
			skin.visible = false;
			skin.mouseChildren = skin.mouseEnabled = false;
			txtTip.textColor = 0x000000;
			txtTip.autoSize = TextFieldAutoSize.LEFT;
			txtTip.wordWrap = true;
			txtTip.width = txtTip.height = 100;
			skin.addChild(txtTip);
			skin.addChild(skinBG);
			return skin;
		}
		
		/**
		 * 通过文字生成带按钮的提示窗，居中，点击按钮后自动关闭。
		 * @param data		提示窗里的文字
		 * @param backFun	返回函数一定要带一个参数,用来接收点的按钮
		 * @param btnStr	通过字符生成按钮
		 * @param pan		提示窗皮肤
		 */
		public function alert(data:Object,btnStr:String="确定|取消",panSkin:Sprite=null,btnCls:Class=null):BaseAlert
		{
			var mask:Sprite = new Sprite();
			mask.graphics.beginFill(0xFFFFFF,0.5);
			mask.graphics.drawRect(0,0,SparrowGUI.stageWidth,SparrowGUI.stageHeight);
			mask.graphics.endFill();
			
			var pan:BaseAlert = new BaseAlert(data,btnStr,panSkin,btnCls);
			pan.update(data);
			PubFun.alignRect(mask,pan,"MC");
			mask.x -= pan.x;
			mask.y -= pan.y;
			pan.addChildAt(mask,0);
			SparrowGUI.mainDisp.addChild(pan);
//			if(backFun!=null) pan.addEventListener(AlertEvent.ALERT_CLOSE,backFun);
			return pan;
		}
		
		// 确定提示
		public function alertOk(data:Object,btnStr:String="确定",panSkin:Sprite=null,btnCls:Class=null):BaseAlert
		{
			return alert(data,btnStr,panSkin,btnCls);
		}
		
		/**
		 * 文字输入窗
		 * @param data			提示窗里的文字
		 * @param defInputTxt	输入框默认文字
		 * @param backFun		返回函数一定要带两个参数,用来接收点的按钮
		 * @param btnStr		通过字符生成按钮
		 * @param pan			提示窗皮肤
		 * @return 
		 */
		public function alertInput(data:Object,defInputTxt:String,btnStr:String="确定|取消",
								   panSkin:Sprite=null,btnCls:Class=null):BaseAlert
		{
			var pan:BaseAlert = alert(data,btnStr,panSkin,btnCls) as BaseAlert;
			pan.txt_input.visible = true;
			pan.txt_input.maxChars = 10;
			pan.txt_input.htmlText = defInputTxt;
			return pan;
		}
		
		/**
		 * 数字提示窗,只是比普通文字框多了个数字规则判断而已。
		 * @return 
		 */
		public function alertNumInput(data:Object,defInputTxt:String,btnStr:String="确定|取消",pan:BaseAlert=null):BaseAlert
		{
			pan = alertInput(data,defInputTxt,btnStr,pan) as BaseAlert;
			pan.txt_input.restrict = '0-9';
			return pan;
		}
	}
}
